package com.skycode5.servlet;

import com.skycode5.bean.NewsBean;
import com.skycode5.dao.NewsDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

    @WebServlet("/newsEdit.jsp")
    public class NewsUpdateServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            String newsTitle = req.getParameter("upd-NewsTitle");
            String newsContent = req.getParameter("upd-NewsContent");
            int newsId = Integer.parseInt(req.getParameter("newsId"));
            NewsBean newsBean = new NewsBean();//实例化新闻对象
            newsBean.setNewsId(newsId);
            newsBean.setNewsTitle(newsTitle);//设置新闻标题
            newsBean.setNewsContent(newsContent);//设置新闻内容
            newsBean.setAdmianName("admin");// todo 登录管理员的名字
            NewsDao newsDao = new NewsDao();//实例化新闻Dao对象
            int result = newsDao.update(newsBean);
            if (result>0) {
                resp.sendRedirect("newsAdd.jsp");//参数是相对网址
            } else {
                req.setAttribute("msg","更新失败");//错误页面显示的内容
                req.setAttribute("url","news.jsp");//显示错误信息后要跳转的页面
                req.getRequestDispatcher("news.jsp").forward(req, resp);
            }
        }
    }